package cn.sc.allinpay.allfavour.module.printer;

import android.text.TextUtils;

import com.landicorp.android.eptapi.device.Printer;
import com.landicorp.android.eptapi.utils.QrCode;

import cn.sc.allinpay.allfavour.app.App;
import cn.sc.allinpay.allfavour.app.Constant;
import cn.sc.allinpay.allfavour.business.common.utils.SpUtils;

/**
 * 收银小票摘要打印
 * author：pany
 * on 2017/11/3 11:48
 */
public class CashierPrinter extends BasePrinter {

    public CashierPrinter(PrintData data) {
        super(data);
    }

    @Override
    protected void printBody(Printer printer, PrintData data) throws Exception {
        printer.setFormat(FormatStyle.createNormalFont());
        printer.println("订 单 号：");
        printer.println(data.getOrderId());
        printer.println("交易时间：" + data.getTime());
        printer.println("订单金额：￥" + data.getAmt());
        if (!TextUtils.isEmpty(data.getTrsAmt())) {
            printer.println("非现金交易金额：￥" + data.getTrsAmt());
        }
        if (!TextUtils.isEmpty(data.getCashAmt())) {
            printer.println("现金交易金额：￥" + data.getCashAmt());
        }
        if (!TextUtils.isEmpty(data.getCardNo())) {
            String cardNo = data.getCardNo();
            printer.println("会员卡号：" + cardNo.substring(0, 4) + "*******" + cardNo.substring(cardNo.length() - 5));
        }
        if (!TextUtils.isEmpty(data.getDisCouponAmt())) {
            printer.println("卡券抵扣金额：￥" + data.getDisCouponAmt() + "元");
        }
        if (!TextUtils.isEmpty(data.getDisRightsAmt())) {
            printer.println("会员优惠金额：￥" + data.getDisRightsAmt() + "元");
        }
        if (!TextUtils.isEmpty(data.getDisEWalletAmt())) {
            printer.println("钱包支付金额：￥" + data.getDisEWalletAmt() + "元");
        }
        String qrStatus = SpUtils.get(App.getContext(), Constant.SPNAME, Constant.SpKey.QR_CODE_STATUS, Constant.QR_CODE_STATUS_VOID_DEFAULT);
        if(TextUtils.isEmpty(qrStatus)) {
            qrStatus = Constant.QR_CODE_STATUS_VOID_DEFAULT;
        }
        if(!TextUtils.isEmpty(data.getCouponUrl()) && qrStatus.equals("开")) {
            printer.println("            领券码");
            QrCode qrCode = new QrCode(data.getCouponUrl() + "?orderNo=" + data.getOrderId(), QrCode.ECLEVEL_Q);
            printer.printQrCode(Printer.Alignment.CENTER, qrCode, 300);
        } else {
            printer.println("");
            printer.feedLine(4);

        }
    }

    @Override
    protected void printFooter(Printer printer, PrintData data) throws Exception {
        printer.feedLine(4);
    }
}
